package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.BlogMapper;
import org.microblog.dao.CollectionMapper;
import org.microblog.dao.CommentMapper;
import org.microblog.dao.LikesMapper;
import org.microblog.entity.Blog;
import org.microblog.entity.Likes;
import org.microblog.service.BlogService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.MyBatisUtil;
import org.microblog.util.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogServiceImpl implements BlogService {
    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
    CommentMapper  commentMapper = sqlSession.getMapper(CommentMapper.class);
    CollectionMapper collectionMapper = sqlSession.getMapper(CollectionMapper.class);
    LikesMapper likesMapper = sqlSession.getMapper(LikesMapper.class);
    @Override
    public Blog getBlogById(int id) throws SQLException {
        try{
            return  blogMapper.findBlogById(id);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int getForwardCountByBid(int bid) throws SQLException {
        try{
            return blogMapper.getForwardCountByBid(bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int deleteBlog(int id) throws SQLException {
        Connection conn = null;
        int result = 0;
        try{
            //删除该条微博的所有评论
            commentMapper.deleteCommentsByBid(id);
            //删除该条微博的所有收藏
            collectionMapper.deleteCollectionByBid(id);
            //删除该条微博的所有点赞
            likesMapper.deleteLikesByBid(id);
            //删除该条微博
            result = blogMapper.deleteBlog(id);
            sqlSession.commit();
        }catch(SQLException e){
            e.printStackTrace();
            if(conn!=null){
                try{
                    conn.rollback();
                }catch(SQLException e1){
                    e.printStackTrace();
                }
            }
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public int getBlogCount(int uId) throws SQLException {
        Connection conn = null;
        try{
            return blogMapper.getBlogCount(uId);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void getAllBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        Connection conn = null;
        try{
            int totalCount = blogMapper.getAllBlogsCount(uId,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = blogMapper.getAllBlogsByPage(uId,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int addNewBlog(Blog blog) throws SQLException {
        Connection conn = null;
        int result;
        try {
            return blogMapper.addNewBlog(blog);
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }


    @Override
    public void getPopBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        Connection conn = null;
        try{

            int totalCount = blogMapper.getPopBlogsCount(uId, keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = blogMapper.getPopBlogsByPage(uId, keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void getMyBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        Connection conn = null;
        try{
            int totalCount = blogMapper.getBlogCountByUidAndKeyword(uId,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = blogMapper.getMyBlogsByPage(uId,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int forwardBlog(int uId, String content, int forwardBlogId) throws SQLException {
        Connection conn = null;
        try{
            Blog forwardBlog = blogMapper.findBlogById(forwardBlogId);
            int fromBid = 0;
            content = (content != null && !content.equals(""))?content:"转发微博";
            if(forwardBlog.getState() == 2){
                //属于转发的微博
                fromBid = forwardBlog.getOrigin().getId();
            }else{
                //属于原创微博
                fromBid = forwardBlog.getId();
            }
            return blogMapper.forwardBlog(uId, content, fromBid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }
}
